package com.tarena.lbs.controller;

import com.tarena.lbs.entity.User;
import com.tarena.lbs.service.WebService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/web")
@Api(value = "Web页面", tags = "Web页面相关操作")
public class WebController {

    @Autowired
    private WebService webService;

    @GetMapping("/users")
    @ApiOperation(value = "用户列表页面", notes = "显示所有用户列表")
    public Map<String, Object> userList() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<User> users = webService.getAllUsers();
            result.put("code", 200);
            result.put("message", "获取用户列表成功");
            result.put("data", users);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取用户列表失败: " + e.getMessage());
        }
        return result;
    }

    @GetMapping("/user/{id}")
    @ApiOperation(value = "用户详情页面", notes = "显示用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    public Map<String, Object> userDetail(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            User user = webService.getUserById(id);
            if (user != null) {
                result.put("code", 200);
                result.put("message", "获取用户详情成功");
                result.put("data", user);
            } else {
                result.put("code", 404);
                result.put("message", "用户不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取用户详情失败: " + e.getMessage());
        }
        return result;
    }

    @GetMapping("/user/edit/{id}")
    @ApiOperation(value = "编辑用户页面", notes = "编辑用户信息页面")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    public Map<String, Object> editUser(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            User user = webService.getUserById(id);
            if (user != null) {
                result.put("code", 200);
                result.put("message", "获取用户信息成功，可进行编辑");
                result.put("data", user);
            } else {
                result.put("code", 404);
                result.put("message", "用户不存在，无法编辑");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取用户信息失败: " + e.getMessage());
        }
        return result;
    }

    @PostMapping("/user/update")
    @ApiOperation(value = "更新用户信息", notes = "更新用户详细信息")
    @ApiImplicitParam(name = "user", value = "用户信息", required = true, dataType = "User", paramType = "body")
    public Map<String, Object> updateUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = webService.updateUser(user);
            if (success) {
                result.put("code", 200);
                result.put("message", "用户信息更新成功");
            } else {
                result.put("code", 500);
                result.put("message", "用户信息更新失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "用户信息更新失败: " + e.getMessage());
        }
        return result;
    }

    @GetMapping("/user/delete/{id}")
    @ApiOperation(value = "删除用户", notes = "根据用户ID删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    public Map<String, Object> deleteUser(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = webService.deleteUser(id);
            if (success) {
                result.put("code", 200);
                result.put("message", "用户删除成功");
            } else {
                result.put("code", 500);
                result.put("message", "用户删除失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "用户删除失败: " + e.getMessage());
        }
        return result;
    }
    @PostMapping("/store/add")
    @ApiOperation(value = "新增门店", notes = "创建新的门店信息")
    @ApiImplicitParam(name = "user", value = "门店信息", required = true, dataType = "User", paramType = "body")
    public Map<String, Object> addStore(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 调用服务层方法添加门店
            boolean success = webService.addStore(user);
            if (success) {
                result.put("code", 200);
                result.put("message", "门店添加成功");
                result.put("data", user.getId()); // 返回新增门店的ID
            } else {
                result.put("code", 500);
                result.put("message", "门店添加失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "门店添加失败: " + e.getMessage());
        }
        return result;
    }
}
